package guoanmaker.operator.business.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.service.CommodityManageService;
import guoanmaker.operator.business.service.OperatorRefuselistService;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.OPERATOR_COMMODITY)
public class OperatorCommodityManageController {
	
	
	@Resource
	private  CommodityManageService commodityManageService;
	
	@Resource
	private  OperatorRefuselistService operatorRefuselistService;
	
	@Resource
	private AjaxJson ajaxJson;
	/**
	 * 
	 * @param page
	 * @param size
	 * @param status   商品状态 1:待审核 2:审核通过 3:审核失败 4:下架
	 * @return
	 */
	@ApiOperation(value = "商品信息列表")
	@RequestMapping(value="selectcommdityInfos",method = RequestMethod.POST)
	public AjaxJson selectcommdityInfos(Integer page,
           Integer size,Integer status){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.selectcommdityInfos(page,size,status));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param response
	 * @param ids
	 */
	//导出商品信息
	@ApiOperation(value = "导出商品信息EXCEL")
	@RequestMapping(value="exportCommdityInfos",method = RequestMethod.POST)
	public @ResponseBody void exportCommdityInfos(HttpServletResponse response, String ids) {
		try (OutputStream out = response.getOutputStream(); Workbook wb = commodityManageService.exportCommdityInfos(ids);) {
			response.setContentType("application/x-msexcel");
			response.setHeader("Content-Disposition", "attachment;filename=exportBusiness.xls");
			wb.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "查询商品信息")
	@RequestMapping(value="selectBycommdityinfo",method = RequestMethod.POST)
	public AjaxJson selectBycommdityinfo(String id){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.selectBycommdityinfo(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	
	
	//多条件查询商品列表
	/**
	 * 
	 * @param page
	 * @param size
	 * @param status           
	 * @param category
	 * @param startPrice
	 * @param endPrice
	 * @param commdittyName
	 * @param busname
	 * @return
	 */
	@ApiOperation(value = "多条件查询商品列表")
	@RequestMapping(value="multiConditionCommditys",method = RequestMethod.POST)
	public AjaxJson multiConditionCommditys( Integer page,
            Integer size,Integer status,String category,Double startPrice,Double endPrice,String commdittyName,String busname,String commodityCode){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.multiConditionCommditys(page,size,status,category,startPrice,endPrice,commdittyName,busname,commodityCode));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "添加商品")
	@RequestMapping(value="addcommdity",method = RequestMethod.POST)
	public AjaxJson addcommdity( String commdittyName){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.addcommdity(commdittyName));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "审核商品")
	@RequestMapping(value="reviewProduct",method = RequestMethod.POST)
	public AjaxJson reviewProduct(String id,String userid){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.reviewProduct(id,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "拒绝原因下拉列表")
	@RequestMapping(value="reasonDropdown",method = RequestMethod.POST)
	public AjaxJson reasonDropdown(){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.reasonDropdown());
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "下架原因下拉列表")
	@RequestMapping(value="reasonxiajia",method = RequestMethod.POST)
	public AjaxJson reasonxiajia(){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.reasonxiajia());
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param id
	 * @param refuseName
	 * @param content
	 * @param userid
	 * @return
	 */
	@ApiOperation(value = "拒绝商品")
	@RequestMapping(value="refuseCommdity",method = RequestMethod.POST)
	public AjaxJson refuseCommdity(String id,String refuseName,String content,String userid){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.refuseCommdity(id,refuseName,content,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param id
	 * @param refuseName
	 * @param content
	 * @return
	 */
	@ApiOperation(value = "下架商品")
	@RequestMapping(value="shelvesCommdity",method = RequestMethod.POST)
	public AjaxJson shelvesCommdity(String id,String refuseName,String content,String userid){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(commodityManageService.shelvesCommdity(id,refuseName,content,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	/********商品审批原因*******/
	@ApiOperation(value = "查询审核商品原因列表加分页", notes = "查询审核商品原因列表加分页")
	@RequestMapping(value="queryrefuseCommListpage",method = RequestMethod.POST)
	public AjaxJson queryrefuseCommListpage(@RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.queryrefuseCommListpage(page,size));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param refuseName
	 * @param userid
	 * @return
	 */
	@ApiOperation(value = "新增商品审核原因（针对商品）")
	@RequestMapping(value="addCommrefuse",method = RequestMethod.POST)
	public AjaxJson addCommrefuse(String refuseName,String userid){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.addCommrefuse(refuseName,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param id
	 * @param refuseName
	 * @param userid
	 * @return
	 */
	@ApiOperation(value = "编辑审核原因（针对商品）")
	@RequestMapping(value="editCommrefuse",method = RequestMethod.POST)
	public AjaxJson editCommrefuse(String id,String refuseName,String userid){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.editCommrefuse(id,refuseName,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value ="批量删除审核原因（针对商品）")
	@RequestMapping(value ="deleteCommrefuse",method = RequestMethod.POST)
	public AjaxJson deleteCommrefuse(String ids){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.deleteCommrefuse(ids));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
   
	@ApiOperation(value = "单个删除审核原因（针对商品）")
	@RequestMapping(value ="deleteCommRefuseList",method = RequestMethod.POST)
	public AjaxJson deleteCommRefuseList(String id){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.deletereUnifuseList(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
/**
 * 
 * @param page
 * @param size
 * @param refuseName
 * @return
 */
	@ApiOperation(value = "按照除审核原因查询（针对商品）")
	@RequestMapping(value ="queryRefuseCommNforList",method = RequestMethod.POST)
	public AjaxJson queryRefuseCommNforList(@RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,String refuseName){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.queryRefuseCommNforList(page,size,refuseName));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/*************下架原因类别**************/
	@ApiOperation(value = "查询下架商品原因列表加分页", notes = "查询下架商品原因列表加分页")
	@RequestMapping(value ="queryrefuseShelvesListpage",method = RequestMethod.POST)
	public AjaxJson queryrefuseShelvesListpage(@RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.queryrefuseShelvesListpage(page,size));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value = "新增商品下架原因（针对商品）")
	@RequestMapping(value ="addShelves",method = RequestMethod.POST)
	public AjaxJson addShelves(String refuseName,String userid){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.addShelves(refuseName,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "编辑下架原因（针对商品）")
	@RequestMapping(value="editShelves",method = RequestMethod.POST)
	public AjaxJson editShelves(String id,String refuseName,String userid){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.editShelves(id,refuseName,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value = "批量删除审核原因（针对商品）")
	@RequestMapping(value="deleteShelvesrefuse",method = RequestMethod.POST)
	public AjaxJson deleteShelvesrefuse(String ids){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.deleteCommrefuse(ids));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
   
	@ApiOperation(value = "单个删除审核原因（针对商品）")
	@RequestMapping(value="deleteShelvesRefuseList",method = RequestMethod.POST)
	public AjaxJson deleteShelvesRefuseList(String id){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.deletereUnifuseList(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param page
	 * @param size
	 * @param refuseName
	 * @return
	 */
	@ApiOperation(value = "按照下架原因查询（针对商品）")
	@RequestMapping(value="queryShelvesNforList",method = RequestMethod.POST)
	public AjaxJson queryShelvesNforList(@RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,String refuseName){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.queryShelvesNforList(page,size,refuseName));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
}
